package com.yingside.io.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TestFileInputStream {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File("aa.txt"));
            /*int read1 = fileInputStream.read();
            System.out.println("read = " + (char)read1);

            int read2 = fileInputStream.read();
            System.out.println("read = " + (char)read2);

            int read3 = fileInputStream.read();
            System.out.println("read = " + (char)read3);

            int read4 = fileInputStream.read();
            System.out.println("read = " + (char)read4);

            int read5 = fileInputStream.read();
            System.out.println("read = " + read5);*/

            int b = -1;

            byte [] bs = new byte[6];

            //每次读取一个字节，直接返回读取到的这个字节，read到文件末尾返回-1
            //int n = fileInputStream.read();
            //每次读取byte类型数组长度的字节，并且把内容存储到数组中，返回值表示这次循环读取的字节的长度
            //如果读取到文件默认，返回-1
            //int m = fileInputStream.read(bs);

            while((b = fileInputStream.read(bs)) != -1){
                //把字节数组转换为String类型
                String str = new String(bs,0,b);
                System.out.print(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
